package com.foreveross.common.TaskManager;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.SYWebService.SYHttpclientCall;
import com.foreveross.SYWebService.vo.ConditionVo;
import com.foreveross.SYWebService.vo.DataVo;
import com.foreveross.SYWebService.vo.SYResult;
import com.foreveross.SYWebService.vo.SYSWin;
import com.foreveross.common.Constants;
import com.foreveross.dao.HouseRepairDAO;
import com.foreveross.util.DateUtil;
import com.foreveross.util.ObjectToMap;
import com.foreveross.vo.HouseRepair;

@Service("houseRepairTaskService")
@Transactional
public class HouseRepairTaskService {
	
	private static Logger log = Logger.getLogger(HouseRepairTaskService.class);
	
	@Resource
	HouseRepairDAO houseRepairDAO;
	
	private Timer timer1 = null; 
	private Timer timer2 = null; 
	
	private final Calendar cal = Calendar.getInstance();
	
	//一天运行一次
	private final long timePeriod = 24*60*60*1000;
	

	public Timer getTimer1() {
		return timer1;
	}
	public Timer getTimer2() {
		return timer2;
	}
	
	public void updateHouseRepairStatusTimer(){
		timer1 = new Timer(); 	          
		timer2 = new Timer(); 	          
	    timer1.schedule(updateHouseRepairStatusTimerTask(), setTime1(), timePeriod); 
	    timer2.schedule(updateHouseRepairStatusTimerTask(), setTime2(), timePeriod); 
	}
	
	private TimerTask updateHouseRepairStatusTimerTask(){
		return new TimerTask() {
	    	 public void run() {  
	    		 //执行的内容   
	    		 try{
	    			 System.out.println("开始报修更新状态...");
	    			 updateHouseRepairStatus();
	    			 System.out.println("结束报修更新状态...");
	    		 }catch(Exception e){
	    			 log.error("报修更新状态异常", e);;
	    			 e.printStackTrace();
	    		 }
	    	 }
	    };
	}

	private Date setTime1(){	

		//每天定点执行     
		cal.set(Calendar.HOUR_OF_DAY,1);     
		cal.set(Calendar.MINUTE,0);    
		cal.set(Calendar.SECOND,0);
		
		
		Date date=cal.getTime(); 
		//第一次执行定时任务的时间 
		//如果第一次执行定时任务的时间 小于 当前的时间  
		//此时要在 第一次执行定时任务的时间 加一天，以便此任务在下个时间点执行。如果不加一天，任务会立即执行。  
		if (date.before(new Date()))  date =DateUtil.addDay(date, 1);   
		
		return date;
	}
	
	private Date setTime2(){	

		//每天定点执行     
		cal.set(Calendar.HOUR_OF_DAY,12);     
		cal.set(Calendar.MINUTE,30);    
		cal.set(Calendar.SECOND,0);
		
		
		Date date=cal.getTime(); 
		//第一次执行定时任务的时间 
		//如果第一次执行定时任务的时间 小于 当前的时间  
		//此时要在 第一次执行定时任务的时间 加一天，以便此任务在下个时间点执行。如果不加一天，任务会立即执行。  
		if (date.before(new Date()))  date =DateUtil.addDay(date, 1);   
		
		return date;
	}


	private void updateHouseRepairStatus() throws Exception{
		List<HouseRepair> houseRepairList = houseRepairDAO.findHouseRepairList(null, null, null, null, null, Constants.REPAIR_UNDEAL, null, null, 0, Constants.PAGE_MAX_SIZE, null);
		for(HouseRepair houseRepair : houseRepairList){
			ConditionVo conditionVo = new ConditionVo();
			conditionVo.setP0("User_GetServiceDetail");
			
			String event_id = houseRepair.getRepairNo();
			String org_id = houseRepair.getHrHouses().getProjectId();
			
			conditionVo.setP1(event_id);
			conditionVo.setP2(org_id);
			System.out.println("报事ID:"+houseRepair.getRepairNo() +"|项目ID:"+houseRepair.getHrHouses().getProjectId());
			if (event_id != null && !event_id.isEmpty() && 
					org_id != null && !org_id.isEmpty()){
				Map<String, String> params = new HashMap<String, String>();
				params = ObjectToMap.objectToMapViaFields(conditionVo, true);
				SYResult sysResult = SYHttpclientCall.getSYData(params);
				List<SYSWin> list = sysResult.getUser_GetServiceDetail();
				if (list != null && !list.isEmpty()) {
					SYSWin sYSWin = list.get(0);
					List<DataVo> dataVoList = sYSWin.getSyswin();
					if(dataVoList != null && dataVoList.size()>0){
						DataVo dataVo = dataVoList.get(0);
						if("无效".equals(dataVo.getWorkOrderState()) ||
						("有效".equals(dataVo.getWorkOrderState()) &&
								"已完成100%".equals(dataVo.getProcessing()))){
							//houseRepair.setStatus(Integer.parseInt(Constants.REPAIR_DEAL));
							houseRepairDAO.updateHouserRepair(houseRepair.getId(),Constants.REPAIR_DEAL);
						}
					}
				}
			} else {
				houseRepairDAO.updateHouserRepair(houseRepair.getId(),Constants.REPAIR_DEAL);
			}
		}
	}
}
